package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.dto.perms.PermsCategory;
import com.abc.cloud.dto.perms.PermsTree;
import com.abc.cloud.vo.perms.PermsSave;
import com.abc.cloud.vo.perms.PermsUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.PermsEntity;
import com.abc.cloud.service.PermsService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 权限表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-11-05 10:18:14
 */
@RestController
@RequestMapping("/perms")
public class PermsController {
    @Autowired
    private PermsService permsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = permsService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		PermsEntity perms = permsService.getById(id);

        return Result.success(perms, "perms");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody PermsEntity perms){
		permsService.save(perms);

        return Result.success(null, "");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody PermsEntity perms){
		permsService.updateById(perms);

        return Result.success(null, "操作成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		permsService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    @GetMapping("/getPermsInfo")
    public Result<PermsEntity> getPermsInfo(@RequestParam("permsId") Long permsId){
        PermsEntity tbPerms = permsService.getPermsInfoById(permsId);
        if(tbPerms == null){
            return Result.fail(null, "权限菜单不存在");
        }
        return Result.success(tbPerms, "查询权限菜单成功");
    }

    @PostMapping("/savePermsInfo")
    public Result<PermsEntity> savePermsInfo(@RequestBody PermsSave permsSave){
        PermsEntity tbPerms = permsService.savePermsInfo(permsSave);
        if(tbPerms == null){
            return Result.fail(null, "保存权限菜单出错");
        }
        return Result.success(tbPerms, "保存权限菜单成功");
    }

    @PutMapping("/updatePermsInfo")
    public Result<PermsEntity> updatePermsInfo(@RequestBody PermsUpdate permsUpdate){
        PermsEntity tbPerms = permsService.updatePermsInfo(permsUpdate);
        if(tbPerms == null){
            return Result.fail(null, "修改权限菜单出错");
        }
        return Result.success(tbPerms, "修改权限菜单成功");
    }

    @DeleteMapping("/deletePermsInfo/{permsId}")
    public Result<Long> deletePerms(@PathVariable("permsId") Long permsId){
        Boolean i = permsService.deletePerms(permsId);
        if(!i){
            return Result.fail(null, "删除权限菜单出错");
        }
        return Result.success(permsId, "删除权限菜单成功");
    }

    @GetMapping("/getPermsList")
    public Result<List<PermsTree>> getPermsList(){
        List<PermsTree> permsTreeList = permsService.getPermsList();
        return Result.success(permsTreeList, "查询所有权限菜单成功");
    }

    @GetMapping("/getPermsCategory")
    public Result<List<PermsCategory>> getPermsCategory(){
        List<PermsCategory> permsList = permsService.getPermsCategory();
        return Result.success(permsList, "获取权限分类成功");
    }



}
